﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;
using System.Xml.Xsl;
using System.IO;
using System.Xml.XPath;
using System.Xml.Schema;
using System.Collections;
using System.Windows;

namespace Rsa
{
    public class GestorConfigXML
    {
        public const int CONFIGURACAO = 1;
        public const int CONFIG_EMENTA = 2;
        public const string CONFIG_FILE = "xml_xsd/config.xml";
        public const string XSD_FILE = "xml_xsd/config.xsd";
        public const string TARGETNAMESPACE = "http://integracao.de.sistemas";
        private static bool _isvalid = true;
        private static string _errmsg;

        private XmlDocument doc;

        public GestorConfigXML()
        {
            //this.checkFileExists(CONFIG_FILE);
            doc = new XmlDocument();
            this.carregaXML();
        }

        #region email
        public bool guardaEmail(string username, string password, 
            string imapServidor, string imapPorta, string imapEncrypt,
            string smtpServidor, string smtpPorta, string smtpEncrypt)
        {

            XmlNodeList nl = doc.SelectNodes("/config/email/escola/username");
            foreach (XmlNode n in nl)
                n.InnerXml = username;

            nl = doc.SelectNodes("/config/email/escola/password");
            foreach (XmlNode n in nl)
                n.InnerXml = password;

            nl = doc.SelectNodes("/config/email/escola/imap_servidor");
            foreach (XmlNode n in nl)
                n.InnerXml = imapServidor;

            nl = doc.SelectNodes("/config/email/escola/imap_porta");
            foreach (XmlNode n in nl)
                n.InnerXml = imapPorta;

            nl = doc.SelectNodes("/config/email/escola/imap_encrypt");
            foreach (XmlNode n in nl)
                n.InnerXml = imapEncrypt;

            nl = doc.SelectNodes("/config/email/escola/imap_servidor");
            foreach (XmlNode n in nl)
                n.InnerXml = imapServidor;

            nl = doc.SelectNodes("/config/email/escola/smtp_servidor");
            foreach (XmlNode n in nl)
                n.InnerXml = smtpServidor;

            nl = doc.SelectNodes("/config/email/escola/smtp_porta");
            foreach (XmlNode n in nl)
                n.InnerXml = smtpPorta;

            nl = doc.SelectNodes("/config/email/escola/smtp_encrypt");
            foreach (XmlNode n in nl)
                n.InnerXml = smtpEncrypt;

            this.saveDoc();
            this.carregaXML();
            return true;
        }

        public Hashtable loadEmail()
        {
            string username = null;
            string password = null;
            string imapServidor = null;
            string imapPorta = null;
            string imapEncrypt = null;
            string smtpServidor = null;
            string smtpPorta = null;
            string smtpEncrypt = null;


            XmlNodeList nl = doc.SelectNodes("/config/email/escola/username");
            foreach (XmlNode n in nl)
                username = n.InnerXml;

            nl = doc.SelectNodes("/config/email/escola/password");
            foreach (XmlNode n in nl)
                password = n.InnerXml;

            nl = doc.SelectNodes("/config/email/escola/imap_servidor");
            foreach (XmlNode n in nl)
                imapServidor = n.InnerXml;

            nl = doc.SelectNodes("/config/email/escola/imap_porta");
            foreach (XmlNode n in nl)
                imapPorta = n.InnerXml;

            nl = doc.SelectNodes("/config/email/escola/imap_encrypt");
            foreach (XmlNode n in nl)
                imapEncrypt = n.InnerXml;

            nl = doc.SelectNodes("/config/email/escola/smtp_servidor");
            foreach (XmlNode n in nl)
                smtpServidor = n.InnerXml;

            nl = doc.SelectNodes("/config/email/escola/smtp_porta");
            foreach (XmlNode n in nl)
                smtpPorta = n.InnerXml;

            nl = doc.SelectNodes("/config/email/escola/smtp_encrypt");
            foreach (XmlNode n in nl)
                smtpEncrypt = n.InnerXml;

            Hashtable hashtable = new Hashtable();
            hashtable.Add("username", username);
            hashtable.Add("password", password);
            hashtable.Add("imapServidor", imapServidor);
            hashtable.Add("imapPorta", imapPorta);
            hashtable.Add("imapEncrypt", imapEncrypt);
            hashtable.Add("smtpServidor", smtpServidor);
            hashtable.Add("smtpPorta", smtpPorta);
            hashtable.Add("smtpEncrypt", smtpEncrypt);

            return hashtable;
        }
        #endregion

        #region moodle
        public bool guardaMoodle(string username, string password)
        {


            XmlNodeList nl = doc.SelectNodes("/config/moodle/username");
            foreach (XmlNode n in nl)
                n.InnerXml = username;

            nl = doc.SelectNodes("/config/moodle/password");
            foreach (XmlNode n in nl)
                n.InnerXml = password;
            
            this.saveDoc();
            return true;
        }

        public Hashtable loadMoodle()
        {
            string username = null;
            string password = null;
           
            XmlNodeList nl = doc.SelectNodes("/config/moodle/username");
            foreach (XmlNode n in nl)
               username = n.InnerXml;

            nl = doc.SelectNodes("/config/moodle/password");
            foreach (XmlNode n in nl)
               password = n.InnerXml;

            Hashtable hashtable = new Hashtable();
            hashtable.Add("username", username);
            hashtable.Add("password", password);

            return hashtable;
        }
        #endregion

        #region twitter

        public bool guardaTwitter(string url, string consumerkey, string consumersecret, string pin, string acesstoken, string acesstokensecret)
        {

            XmlNodeList nl = doc.SelectNodes("/config/twitter/url");
            foreach (XmlNode n in nl)
                n.InnerXml = url;

            nl = doc.SelectNodes("/config/twitter/consumerkey");
            foreach (XmlNode n in nl)
                n.InnerXml = consumerkey;

            nl = doc.SelectNodes("/config/twitter/consumersecret");
            foreach (XmlNode n in nl)
                n.InnerXml = consumersecret;

            nl = doc.SelectNodes("/config/twitter/pin");
            foreach (XmlNode n in nl)
                n.InnerXml = pin;

            nl = doc.SelectNodes("/config/twitter/acesstoken");
            foreach (XmlNode n in nl)
                n.InnerXml = acesstoken;

            nl = doc.SelectNodes("/config/twitter/acesstokensecret");
            foreach (XmlNode n in nl)
                n.InnerXml = acesstokensecret;


            this.saveDoc();
            return true;
        }

        public bool guardaTwitter(string url, string consumerkey, string consumersecret, string acesstoken, string acesstokensecret)
        {

            XmlNodeList nl = doc.SelectNodes("/config/twitter/url");
            foreach (XmlNode n in nl)
                n.InnerXml = url;

            nl = doc.SelectNodes("/config/twitter/consumerkey");
            foreach (XmlNode n in nl)
                n.InnerXml = consumerkey;

            nl = doc.SelectNodes("/config/twitter/consumersecret");
            foreach (XmlNode n in nl)
                n.InnerXml = consumersecret;

            nl = doc.SelectNodes("/config/twitter/acesstoken");
            foreach (XmlNode n in nl)
                n.InnerXml = acesstoken;

            nl = doc.SelectNodes("/config/twitter/acesstokensecret");
            foreach (XmlNode n in nl)
                n.InnerXml = acesstokensecret;


            this.saveDoc();
            return true;
        }

        public bool guardaTwitterAcess(string acesstoken, string acesstokensecret)
        {

            XmlNodeList nl = doc.SelectNodes("/config/twitter/acesstoken");
            foreach (XmlNode n in nl)
                n.InnerXml = acesstoken;

            nl = doc.SelectNodes("/config/twitter/acesstokensecret");
            foreach (XmlNode n in nl)
                n.InnerXml = acesstokensecret;


            this.saveDoc();
            return true;
        }

        public bool guardaTwitterId(string acesstoken, string acesstokensecret, string userId)
        {

            XmlNodeList nl = doc.SelectNodes("/config/twitter/acesstoken");
            foreach (XmlNode n in nl)
                n.InnerXml = acesstoken;

            nl = doc.SelectNodes("/config/twitter/acesstokensecret");
            foreach (XmlNode n in nl)
                n.InnerXml = acesstokensecret;

            nl = doc.SelectNodes("/config/twitter/userid");
            foreach (XmlNode n in nl)
                n.InnerXml = userId;

            this.saveDoc();
            return true;
        }

        public bool guardaTwitterRequest(string requestacesstoken, string requestacesstokensecret)
        {

            XmlNodeList nl = doc.SelectNodes("/config/twitter/requestacesstoken");
            foreach (XmlNode n in nl)
                n.InnerXml = requestacesstoken;

            nl = doc.SelectNodes("/config/twitter/requestacesstokensecret");
            foreach (XmlNode n in nl)
                n.InnerXml = requestacesstokensecret;


            this.saveDoc();
            return true;
        }

        public bool guardaFacebook(string acesstoken ) {


            XmlNodeList nl = doc.SelectNodes("/config/facebook/acesstoken");
            foreach (XmlNode n in nl)
                n.InnerXml =acesstoken ;

            this.saveDoc();
            return true;

        }

        public Hashtable loadFacebook() {

            string loginurl = null;
            string acesstoken= null;
            string uri = null;
            string appid = null;

            XmlNodeList nl = doc.SelectNodes("/config/facebook/loginurl");

            foreach (XmlNode n in nl)
                loginurl = n.InnerXml;

            nl = doc.SelectNodes("/config/facebook/acesstoken");
            foreach (XmlNode n in nl)
                acesstoken = n.InnerXml;

            nl = doc.SelectNodes("/config/facebook/uri");
            foreach (XmlNode n in nl)
                uri = n.InnerXml;

            nl = doc.SelectNodes("/config/facebook/appid");
            foreach (XmlNode n in nl)
                appid = n.InnerXml;

            Hashtable hashtable = new Hashtable();
            hashtable.Add("loginurl", loginurl);
            hashtable.Add("acesstoken", acesstoken);
            hashtable.Add("uri", uri);
            hashtable.Add("appid", appid);


            return hashtable;

        }

        public Hashtable loadTwitter()
        {
            string url = null;
            string consumerkey = null;
            string consumersecret = null;
            string pin = null;
            string acesstoken = null;
            string acesstokensecret = null;
            string requestacesstoken = null;
            string requestacesstokensecret = null;
            string userid = null;

            XmlNodeList nl = doc.SelectNodes("/config/twitter/url");

            foreach (XmlNode n in nl)
                url = n.InnerXml;

            nl = doc.SelectNodes("/config/twitter/consumerkey");
            foreach (XmlNode n in nl)
                consumerkey = n.InnerXml;

            nl = doc.SelectNodes("/config/twitter/consumersecret");
            foreach (XmlNode n in nl)
                consumersecret = n.InnerXml;

            nl = doc.SelectNodes("/config/twitter/pin");
            foreach (XmlNode n in nl)
                pin = n.InnerXml;

            nl = doc.SelectNodes("/config/twitter/acesstoken");
            foreach (XmlNode n in nl)
                acesstoken = n.InnerXml;

            nl = doc.SelectNodes("/config/twitter/acesstokensecret");
            foreach (XmlNode n in nl)
                acesstokensecret = n.InnerXml;

            nl = doc.SelectNodes("/config/twitter/requestacesstoken");
            foreach (XmlNode n in nl)
                requestacesstoken = n.InnerXml;

            nl = doc.SelectNodes("/config/twitter/requestacesstokensecret");
            foreach (XmlNode n in nl)
                requestacesstokensecret = n.InnerXml;

            nl = doc.SelectNodes("/config/twitter/userid");
            foreach (XmlNode n in nl)
                userid = n.InnerXml;

            Hashtable hashtable = new Hashtable();
            hashtable.Add("url", url);
            hashtable.Add("consumerkey", consumerkey);
            hashtable.Add("consumersecret", consumersecret);
            hashtable.Add("pin", pin);
            hashtable.Add("acesstoken", acesstoken);
            hashtable.Add("acesstokensecret", acesstokensecret);
            hashtable.Add("requestacesstoken", requestacesstoken);
            hashtable.Add("requestacesstokensecret", requestacesstokensecret);
            hashtable.Add("userid", userid);

            return hashtable;
        }

        #endregion

        #region geral

        public Hashtable loadGeral()
        {
            string encrypt=null;
            XmlNodeList nl = doc.SelectNodes("/config/configGeral/encrypt");
            foreach (XmlNode n in nl)
                encrypt = n.InnerXml;


            Hashtable hashtable = new Hashtable();
            hashtable.Add("encrypt", encrypt);

            return hashtable;
        }

        public bool guardaGeral(int encrypt)
        {
            XmlNodeList nl = doc.SelectNodes("/config/configGeral/encrypt");
            foreach (XmlNode n in nl)
            {
                n.InnerXml = encrypt.ToString();
            }
            this.saveDoc();
            return true;
        }

        #endregion

        #region ementa

        public Hashtable loadEmenta()
        {
            string cantina1_url = null;
            string cantina2_url = null;
            string cantina3_url = null;
            string cantina4_url = null;
            string cantina5_url = null;
            string cantina1_cb = null;
            string cantina2_cb = null;
            string cantina3_cb = null;
            string cantina4_cb = null;
            string cantina5_cb = null;
            string dias_mostrar = null;

            XmlNodeList nl = doc.SelectNodes("/config/ementa/cantina1_url");
            foreach (XmlNode n in nl)
                cantina1_url = n.InnerXml;

            nl = doc.SelectNodes("/config/ementa/cantina2_url");
            foreach (XmlNode n in nl)
                cantina2_url = n.InnerXml;

            nl = doc.SelectNodes("/config/ementa/cantina3_url");
            foreach (XmlNode n in nl)
                cantina3_url = n.InnerXml;

            nl = doc.SelectNodes("/config/ementa/cantina4_url");
            foreach (XmlNode n in nl)
                cantina4_url = n.InnerXml;

            nl = doc.SelectNodes("/config/ementa/cantina5_url");
            foreach (XmlNode n in nl)
                cantina5_url = n.InnerXml;

            nl = doc.SelectNodes("/config/ementa/cantina1_cb");
            foreach (XmlNode n in nl)
                cantina1_cb = n.InnerXml;

            nl = doc.SelectNodes("/config/ementa/cantina2_cb");
            foreach (XmlNode n in nl)
                cantina2_cb = n.InnerXml;

            nl = doc.SelectNodes("/config/ementa/cantina3_cb");
            foreach (XmlNode n in nl)
                cantina3_cb = n.InnerXml;

            nl = doc.SelectNodes("/config/ementa/cantina4_cb");
            foreach (XmlNode n in nl)
                cantina4_cb = n.InnerXml;

            nl = doc.SelectNodes("/config/ementa/cantina5_cb");
            foreach (XmlNode n in nl)
                cantina5_cb = n.InnerXml;

            nl = doc.SelectNodes("/config/ementa/dias_mostrar");
            foreach (XmlNode n in nl)
                dias_mostrar = n.InnerXml;

            Hashtable hashtable = new Hashtable();
            hashtable.Add("cantina1_url", cantina1_url);
            hashtable.Add("cantina2_url", cantina2_url);
            hashtable.Add("cantina3_url", cantina3_url);
            hashtable.Add("cantina4_url", cantina4_url);
            hashtable.Add("cantina5_url", cantina5_url);
            hashtable.Add("cantina1_cb", cantina1_cb);
            hashtable.Add("cantina2_cb", cantina2_cb);
            hashtable.Add("cantina3_cb", cantina3_cb);
            hashtable.Add("cantina4_cb", cantina4_cb);
            hashtable.Add("cantina5_cb", cantina5_cb);
            hashtable.Add("dias_mostrar", dias_mostrar);

            return hashtable;
        }

        public bool guardaEmenta(string cantina1_url, string cantina2_url, string cantina3_url, string cantina4_url, string cantina5_url, int cantina1_cb, int cantina2_cb, int cantina3_cb, int cantina4_cb, int cantina5_cb, int dias_mostrar)
        {
            XmlNodeList nl = doc.SelectNodes("/config/ementa/cantina1_url");
            foreach (XmlNode n in nl)
                n.InnerXml = cantina1_url;

            nl = doc.SelectNodes("/config/ementa/cantina2_url");
            foreach (XmlNode n in nl)
                n.InnerXml = cantina2_url;

            nl = doc.SelectNodes("/config/ementa/cantina3_url");
            foreach (XmlNode n in nl)
                n.InnerXml = cantina3_url;

            nl = doc.SelectNodes("/config/ementa/cantina4_url");
            foreach (XmlNode n in nl)
                n.InnerXml = cantina4_url;

            nl = doc.SelectNodes("/config/ementa/cantina5_url");
            foreach (XmlNode n in nl)
                n.InnerXml = cantina5_url;

            nl = doc.SelectNodes("/config/ementa/cantina1_cb");
            foreach (XmlNode n in nl)
                n.InnerXml = cantina1_cb.ToString();

            nl = doc.SelectNodes("/config/ementa/cantina2_cb");
            foreach (XmlNode n in nl)
                n.InnerXml = cantina2_cb.ToString();

            nl = doc.SelectNodes("/config/ementa/cantina3_cb");
            foreach (XmlNode n in nl)
                n.InnerXml = cantina3_cb.ToString();

            nl = doc.SelectNodes("/config/ementa/cantina4_cb");
            foreach (XmlNode n in nl)
                n.InnerXml = cantina4_cb.ToString();

            nl = doc.SelectNodes("/config/ementa/cantina5_cb");
            foreach (XmlNode n in nl)
                n.InnerXml = cantina5_cb.ToString();

            nl = doc.SelectNodes("/config/ementa/dias_mostrar");
            foreach (XmlNode n in nl)
                n.InnerXml = dias_mostrar.ToString();

            this.saveDoc();
            this.carregaXML();
            return true;
        }

        #endregion

        public bool saveDoc()
        {
            doc.Save(CONFIG_FILE);
            return true;
        }

        private bool carregaXML()
        {
            this.doc.Load(CONFIG_FILE);
            return true;
        }

        private bool checkFileExists(string ficheiro){

            if (File.Exists(ficheiro))
            {
                Debug.WriteLine("file exists");
                return true;
            }
            else
            {
               return this.criarFicheiroXML(CONFIG_FILE);
            }
        }
        private bool criarFicheiroXML(string ficheiro)
        {

            XmlTextWriter textWriter = new XmlTextWriter(ficheiro, null);
            textWriter.Formatting = Formatting.Indented;
            XmlQualifiedName qname = new XmlQualifiedName("config",TARGETNAMESPACE);
            //Microsoft.Xml.XMLGen.XmlSampleGenerator generator = new Microsoft.Xml.XMLGen.XmlSampleGenerator(XSD_FILE, qname);
            //generator.WriteXml(textWriter);

            return true;
        }


        public void validarXSD(){

            this.doc = new XmlDocument();

            try
            {

                doc.Schemas.Add(null, XSD_FILE);
                doc.Load(CONFIG_FILE);

                ValidationEventHandler eh = new ValidationEventHandler(validationEventHandler);

                doc.Validate(eh);
                //Console.WriteLine("Documento bem Formatado");
                if (_isvalid)
                {
                    //Console.WriteLine("Documento Valido");
                    //MessageBox.Show("XML não validou com o XSD");
                }
                else
                {
                    
                    //Console.WriteLine("Documento Inválido" + _errmsg);
                    MessageBox.Show("Docemuento Invalido" + _errmsg);
                }
            }
            catch (XmlException e)
            {
              // DEU BODE
                //Console.WriteLine("Documento mal Formado" + e);
                MessageBox.Show("XML não validou com o XSD" + e);
                
            }

            doc.Save(CONFIG_FILE);

        }

        static void validationEventHandler(Object Sender, ValidationEventArgs x)
        {
            _isvalid = false;
            _errmsg = x.Message;


        }

        public string getEmentaURL()
        {
            string res = null;
            XmlNodeList nl = doc.SelectNodes("/config/ementa/url");
            foreach (XmlNode n in nl)
            {
                //Debug.WriteLine(n.InnerXml);
                res = n.InnerXml;
            }
            return res;
        }
    }


}
